﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Douban.Api.Infrastructure;

namespace Douban.Api.Authorization
{
    public class AuthorizationServiceProvider
    {
        private readonly OAuthAuthorization _oauthAuthorization;
        private IAccessTokenManager _tokenManager;
        private const string CheckAccessTokenUrl = "http://api.douban.com/access_token/";

        public DoubanContext Context { get; set; }

        public AuthorizationServiceProvider(OAuthAuthorization oauthAuthorization, IAccessTokenManager tokenManager)
        {
            _oauthAuthorization = oauthAuthorization;
            _tokenManager = tokenManager;
        }

        public Uri BeginAuthorize()
        {
            return _oauthAuthorization.BeginAuthorize();
        }

        public void CompleteAuthorize()
        {
            _oauthAuthorization.CompleteAuthorize();
        }

        public bool IsAccessTokenAvailable
        {
            get
            {
                return _tokenManager.IsTokenAvailable;
                //try
                //{
                //    var response = Context.GetProtectedResource(CheckAccessTokenUrl + _tokenManager.GetToken().Key);
                //    return _tokenManager.IsTokenAvailable && response.StatusCode == HttpStatusCode.OK;
                //}
                //catch (Exception e)
                //{
                //    return false;
                //}
            }
        }
    }
}
